//app.js
const md5 = require("utils/md5.js")
App({
  onLaunch: function () {
    const updateManager = wx.getUpdateManager()

    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
    })

    updateManager.onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '发现新版本，点击确定后将重启',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })
    })

    updateManager.onUpdateFailed(function () {
      // 新版本下载失败
    })
  },
  AdminInfo: {Score:0},
  Keyword: '',
  IsFlash:true,
  User:{ID:0,Score:0,UserID:''},
  Ajax: function (data, callback) {
    //wx.setStorageSync("userid", "");
    data.sign = md5.GetSign(data.act);
    data.token = '26F7BA183C7407E9';
    data.userid = wx.getStorageSync('userid') || '';
    wx.request({
      url: 'https://shop.qiyedaishu.com/tools/API.ashx', //仅为示例，并非真实的接口地址
      //url: 'http://localhost:6159/tools/API.ashx',
      //url: 'http://192.168.1.124/tools/API.ashx',
      //url:'http://192.168.31.49:8080/tools/API.ashx',
      //url: 'http://192.168.43.123/tools/API.ashx',
      //url:'http://192.168.31.49:8080/tools/API.ashx',
      data: data,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (callback) {
          callback(res.data)
        }
      },
      fail:function(res){
        wx.hideLoading();
        wx.showToast({
          title: '网络已经中断',
          image:'/image/error.png'
        })
      }
    })
    
  },
  Login: function(fun) {
      var that = this;
      var userid = wx.getStorageSync('userid') || '';
      // wx.setStorageSync("userid", ""); //如果需要，可以清除用户信息
      if (userid == '') {
      wx.hideLoading();
      wx.navigateTo({
        url: '/pages/login/login',
      })
    } else {
      var data = {};
      data.act = "Login";
      data.userid = userid;
      Ajax(data, function (json) {
        wx.setStorageSync("userid", json.data.UserID);
        app.User = json.data;
        if (fun) { fun(json); }
      });
    }
  },
  
  showError: function (title) {
    wx.showToast({
      title: title,
      image: '/image/error.png'
    })
  },
  showSuccess: function (title) {
    wx.showToast({
      title: title
    })
  }
})